package com.productivity.model;

import java.util.ArrayList;
import java.util.List;

import com.productivity.model.common.Direction;
import com.productivity.model.common.DrawType;
import com.productivity.model.common.Size3D;
import com.productivity.model.common.Vertex3D;

public class Map implements PlayerActionListener {
	private ArrayList<GameObject> objects;
	private Player player;
	public Map() {
		player = new Player(this);
		objects = new ArrayList<GameObject>();
		objects.add(player);
		objects.add(new Platform(new Vertex3D(15,-0.25f,0), new Size3D(50f,0.5f,30)));
		objects.add(new Platform(new Vertex3D(7,3.5f,0), new Size3D(5f,0.5f,3)));
	}
	public void update() {
		for(int i = 0; i < objects.size();i++) {
			GameObject go = objects.get(i);
			go.update(this);
			if(go instanceof DynamicGameObject)
				physicsCheck((DynamicGameObject)go);
			if(go instanceof Enemy)
				if(((Enemy)go).isDead())
					objects.remove(i--);
		}
	}
	public void physicsCheck(DynamicGameObject obj) {
		for(GameObject go: objects) {
			if(obj.getBounds().intersects(go.getBounds())) {
				if(go instanceof StaticGameObject) {
					if(obj.getLocation().getY()-obj.getSize().getY()/2>go.getLocation().getY()) {
						obj.setLocation(new Vertex3D(obj.getLocation().getX(),go.getLocation().getY() + go.getSize().getY()/2 + obj.getSize().getY()/2,obj.getLocation().getZ()));
						if(obj.getVelocity().getY()<0)
							obj.setVelocity(new Vertex3D(obj.getVelocity().getX(),0,obj.getVelocity().getX()));
					}
					else if(obj.getLocation().getX() < go.getLocation().getX())
						obj.getLocation().setX(go.getLocation().getX()-go.getSize().getX()/2-obj.getSize().getX()/2);
					else
						obj.getLocation().setX(go.getLocation().getX()+go.getSize().getX()/2+obj.getSize().getX()/2);
				}
			}
		}
		obj.getVelocity().setY(obj.getVelocity().getY()-.005f);
	}
	public Player getPlayer() {
		return player;
	}
	public List<GameObject> getObjectsofType(DrawType type) {
		ArrayList<GameObject> ret = new ArrayList<GameObject>();
		for(GameObject go:objects)
			if(go.getDrawType()==type)
				ret.add(go);
		return ret;
	}
	@Override
	public void playerAction(PlayerActionType type) {
		
		
	}
}
